package kz.kstu.shop.app.util.locator;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.util.Properties;

/**
 * @author Filipp_Stankevich
 *         created: 28.07.11 21:23
 */
public class EJBRemoteServiceLocator extends AbstractServiceLocator {

    private final static String REMOTE_BEAN_POSTFIX = "Remote";

    private final static String CONTEXT_FACTORY = "org.openejb.client.RemoteInitialContextFactory";
    private final static String PROVIDER_URL = "ejbd://localhost:4201";

    private InitialContext initialContext;

    public EJBRemoteServiceLocator() {}

    @Override
    protected String getServiceFullName(String serviceName) {
        return serviceName + REMOTE_BEAN_POSTFIX;
    }

    @Override
    protected InitialContext getInitialContext() {
        if (initialContext == null) {
             //TODO: need read properties from configuration file through helper class
            Properties properties = new Properties();
            properties.setProperty(Context.INITIAL_CONTEXT_FACTORY, CONTEXT_FACTORY);
            properties.setProperty(Context.PROVIDER_URL, PROVIDER_URL);
            try {
                initialContext = new InitialContext(properties);
            } catch (NamingException e) {
                //TODO: replace own exception
                throw new RuntimeException(e);
            }
        }
        return initialContext;
    }
}
